/**
 * 
 */
package fr.sinno.wms.batch.step;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import fr.sinno.wms.batch.dao.CommandeRepository;
import fr.sinno.wms.batch.model.LineCommand;

/**
 * @author HP
 *
 */
@Service("commandInitTasklet")
public class CommandInitTasklet implements Tasklet {

	private static final Logger LOG = Logger
	.getLogger(CommandInitTasklet.class);

	@Autowired
	private CommandeRepository commandRepository;
	
	@Value("${batch.data.begin.value}")
	private String beginValue;

	@Value("${batch.data.end.value}")
	private String endValue;

	@Override
	public RepeatStatus execute(StepContribution contribution,
			ChunkContext chunkContext) throws Exception {
	    
		LOG.info("####################################################################################");
		LOG.info("###################  DEBUT TRAITEMENT SOR  #########################################");
		LOG.info("####################################################################################");

		List<LineCommand> commandList = commandRepository.findCommandExported(beginValue, endValue);

		chunkContext.getStepContext().getStepExecution().getJobExecution()
		.getExecutionContext().put("commandList", commandList);

		LOG.info("NOMBRE DE COMMANDES POSSIBLES A EXPORTER : " + commandList.size());
		return RepeatStatus.FINISHED;
	}

	public CommandeRepository getCommandRepository() {
		return commandRepository;
	}

	public void setCommandRepository(CommandeRepository commandRepository) {
		this.commandRepository = commandRepository;
	}

}
